<!DOCTYPE html>
<html lang="zh-CN"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>订单列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/style/admin.css" media="all">
	<link rel="stylesheet" href="/style/list.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-input-inline" style="width:100px;">
                    <select name="zoneId" id="zoneId"  lay-verify="required" lay-filter="zoneId" class="select" lay-verify="type">
                        <option value="0">商品专区</option>
                        <option value="11">零售专区</option>
                        <option value="12">体验专区</option>
                        <option value="13">兑换专区</option>
                        <option value="14">活动专区</option>
                        <option value="15">新品专区</option>
                        <option value="16">精品专区</option>
                        <option value="17">开店商品专区</option>
                        <option value="18">开店保证金专区</option>
                        <option value="19">拼团区</option>
                    </select>
                </div>
                <div class="layui-input-inline" style="width:100px;">
                    <select name="seriesId" id="seriesId"  lay-verify="seriesId" class="select" lay-verify="type">
                        <option selected="selected" value="0">商品系列</option>
                        <option value="301">美友系列</option>
                        <option value="302">传奇天露</option>
                    </select>
                </div>
                            <div class="layui-input-inline" style="width:100px;">
                                <select name="cfId1" id="cfId1" lay-filter="cfId1" class="select" lay-verify="type">
                                </select>
                            </div>
                <div class="layui-input-inline" style="width:100px;">
                    <select name="cfId2" id="cfId2" lay-filter="cfId2" class="select" lay-verify="type">
                    </select>
                </div>
                <div class="layui-input-inline" style="width:100px;">
                    <select name="cfId3" id="cfId3" lay-filter="cfId3" class="select" lay-verify="type">
                    </select>
                </div>
                            <div class="layui-input-inline"style="width:100px;">
                                <input type="checkbox" class="switch_checked" lay-filter="stateS" lay-skin="switch" lay-text="上架|下架" checked>
                                <input type="hidden" id="state" value="2">
                            </div>
				<div class="layui-inline">
                    <label class="layui-form-label">商品名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" id="name" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
				<div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search" onclick="SelectData()">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
				<div class="layui-inline" style="float:right">

                </div>
            </div>
        </div>

        <div class="layui-card-body">

            <table class="layui-hide" id="test"></table>
			
			<script type="text/html" id="barStatus">
                <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="detail"  onclick="WinStatus({{d.id}},{{d.state}})">{{d.state==1 ? '上架':'下架'}}</a>
            </script>
			
			<script type="text/html" id="barPic">
                <img src="{{d.pic}}" width="60" height="20" onclick="WinPic('{{d.pic}}');" />
            </script>

            <script type="text/html" id="barSort">
                <input type="text" name="sort{{d.id}}" id="sort{{d.id}}" autocomplete="off" class="layui-input" style="width: 30px; height: 25px; padding: 0;margin: 0;" value="{{d.sort}}" onchange="WinSort({{d.id}})">
            </script>

            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail"  onclick="WinOpenAll({{d.id}},'{{d.name}}')">规格管理</a>

            </script>

        </div>
    </div>
</div>
<script src="/layui/layui.js"></script>
<script src="/layui/jquery.min.js"></script>
<script src="/js/common.js"></script>
<script>
    var table;

    var form1;

    layui.use('table', function(){
        table = layui.table
            ,form = layui.form;
        form1=form;
        table.render({
            elem: '#test'
            ,url:'/goods?url=Goods%2FList&audit=1'
			,cellMinWidth:80
            ,cols: [[
                {field:'sort',width:60, title:'排序', templet: '#barSort', unresize: true}
                ,{field:'id',width:70,title:'ID'}
                ,{field:'zonename',width:120,title:'商品专区'}
				,{field:'name',title:'商品名称'}
				,{field:'title',width:200,title:'商品标题'}
				,{field:'code',width:150,title:'商品编号'}
                ,{field:'cfname', width:100,title:'商品类别'}
				,{field:'pic',width:100, title:'商品图片', templet: '#barPic', unresize: true}
				,{field:'state',width:80,title:'状态', templet: '#barStatus', unresize: true}
                ,{field:'supplier',width:200, title: '供 货 商'}
                ,{field:'addtime',width:200,title: '添加时间',templet :function (row){return createTime(row.addtime);}}
                ,{field:'edit',width:100, title:'编辑', templet: '#barDemo', unresize: true}
            ]]
            ,page: true
            ,id: 'testReload'
        });

        //监听表格复选框选择
        table.on('checkbox(demo)', function(obj){
            console.log(obj)
        });

        form.on('switch(stateS)', function (data) {
            //开关是否开启，true或者false
            var checked = data.elem.checked;
            if (checked) {
                $("#state").val(1);
            }
            else {
                $("#state").val(0);
            }
        });

        form.on('select(cfId1)', function (data) {
            var option2 = $("<option>").val(0).text("选择分类");
            $("#cfId3").html(option2);
            loadfy(data.value, 2);
        });

        form.on('select(cfId2)', function (data) {
            loadfy(data.value, 3);
        });

        loadfy(2,1);

    });

    function createTime(v){
        return new Date(v).toLocaleString()
    }
    /**重写toLocaleString方法*/
    Date.prototype.toLocaleString = function() {
        var y = this.getFullYear();
        var m = this.getMonth()+1;
        m = m<10?'0'+m:m;
        var d = this.getDate();
        d = d<10?("0"+d):d;
        var h = this.getHours();
        h = h<10?("0"+h):h;
        var M = this.getMinutes();
        M = M<10?("0"+M):M;
        var S=this.getSeconds();
        S=S<10?("0"+S):S;
        return y+"-"+m+"-"+d+" "+h+":"+M+":"+S;}

    function loadfy(pid,m) {
        $.ajax({
            type: "POST",
            url: "/goodsjson",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(GetJsonDataS11(pid)),
            dataType: "json",
            success: function (data) {
                if (data.code == "000000") {
                    var option1 = $("<option>").val(0).text("选择分类");
                    $("#cfId"+m).html(option1);
                    $.each(data.data, function (i, item) {
                        var option1 = $("<option>").val(item.id).text(item.name);
                        $("#cfId"+m).append(option1);
                    });

                    form1.render('select');
                }
                else {
                    alert(data.errorDetail);
                }
            },
            error: function (message) {
                alert("提交数据失败！"+message);
            }
        });
    }

    function GetJsonDataS11(pid) {
        var json = {
            "url": "Classification/List",
            "parm":{"pid":pid,"state":1}
        };
        return json;
    }

	/*添加商品*/
	function WinOpenAdd(){
		window.parent.WinOpenAdd(this,"/goods/goods2/add.html","添加商品",1350,800,1);
	}
	
	/*编辑商品*/
	function WinOpenEdit(d){
		window.parent.WinOpenEdit(this,"/goods/goods2/edit.html?id="+d,"修改商品资料",1300,700,1);
	}
	
	/*删除商品*/
	function WinOpenDel(goodsId){
		window.parent.WinOpenEdit(this,"/goods/goods2/del.html?gid="+goodsId,"删除商品",340,300,1);
	}
	
	/*修改商品状态*/
    function WinStatus(id,state){
        return;
        layer.msg('确定修改吗？', {
            btn: ['确定', '取消']
            ,yes: function(index, layero){
                if(state==1)
                {
                    state=0;
                }
                else
                {
                    state=1;
                }
                $.ajax({
                    type: "POST",
                    url: "/goodsjson",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(GetJsonDataS1(id,state)),
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        if (data.code == "000000") {
                            //alert("提交成功!!");
                            // location.reload();//刷新数据
                            ReloadSelectData();
                        }
                        else {
                            alert(data.errorDetail);
                            return false;
                        }
                    },
                    error: function (message) {
                        alert("提交数据失败！"+message);
                        return false;
                    }
                });
                return true;

            }
        });
    }

    /*修改商品状态*/
    function WinSort(id){
        var sort= $("#sort"+id).val();
        var json = {
            "url": "Goods/Set",
            "parm":{"id":id,
                "sort":sort
            }
        };

        $.ajax({
            type: "POST",
            url: "/goodsjson",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(json),
            dataType: "json",
            async: false,
            success: function (data) {
                if (data.code == "000000") {
                    //alert("提交成功!!");
                    // location.reload();//刷新数据
                    ReloadSelectData();
                }
                else {
                    alert(data.errorDetail);
                }
            },
            error: function (message) {
                alert("提交数据失败！"+message);
            }
        });
    }

    function GetJsonDataS1(id,state) {
        var json = {
            "url": "Goods/Set",
            "parm":{"id":id,
                "state":state
            }
        };
        return json;
    }
	
	/*查看商品图片*/
	function WinPic(pic){
		layer.msg("<img src='"+pic+"' width='300' height='300' />");
	}


    var state1=2;
    var zoneId1 = 0;
    var cfId11 = 0;
    var cfId21 = 0;
    var cfId31 = 0;
    var seriesId1 = 0;
    var name1;
    var page1=1;

    function SelectData() {
        //数据刷新
        state1 = $('#state').val();
        zoneId1 = $('#zoneId').val();
        cfId11 = $('#cfId1').val();
        cfId21 = $('#cfId2').val();
        cfId31 = $('#cfId3').val();
        seriesId1 = $('#seriesId').val();
        name1 = $('#name').val();
        page1=1;
        SelectData10();
    }

    function ReloadSelectData()
    {
        page1 = $(".layui-laypage-em").next().html();
        SelectData10();
    }

    function SelectData10() {
        var url="/goods?url=Goods%2FList&audit=1&";
        if(state1!=2)
		{
			url+="state="+state1+"&";
		}
        if(zoneId1>0)
		{
			url+="zoneId="+zoneId1+"&";
		}
        if(cfId1>0)
		{
            url+="cfId1="+cfId11+"&";
            if(cfId2>0)
            {
                url+="cfId2="+cfId21+"&";
                if(cfId3>0)
                {
                    url+="cfId3="+cfId31+"&";
                }
            }
		}
        if(seriesId1>0)
		{
			url+="seriesId="+seriesId1+"&";
		}
        if(name1!=null && $.trim(name1)!="")
		{
			url+="name="+name1+"&";
		}
		//alert(url);
        //执行重载
        table.reload('testReload', {
            url:url,
            page: {
                curr: page1 //重新从第 1 页开始
            }
        });
    }

    function WinOpenAll(id,name)
    {
        //弹出即全屏
        var index = layer.open({
            type: 2,
            title: '商品规格管理',
            content: '/goods/spec2/list.html?goodsId='+id+"&goodsName="+encodeURI(name),
            area: ['1800px', '500px'],
            maxmin: true
        });
        layer.full(index);
    }

    /**
     *
     * 添加商品
     *
     */
    function Add(body) {
        var zoneId = body.find("#zoneId").val();
        if(zoneId==0)
        {
            alert("请选择商品专区");
            return false;
        }
        var cfId1 = body.find("#cfId1").val();
        var cfId2 = body.find("#cfId2").val();
        var cfId3 = body.find("#cfId3").val();
        if(cfId3==null || cfId3==0)
        {
            alert("请选择商品分类");
            return false;
        }
        var cfname = body.find("#cfname").val();
        var zonename = body.find("#zonename").val();
        var pic = body.find("#pic").val();
        var ipic = body.find("#ipic").val();
        var piclist = body.find("#picList").val();
        var picdetails = body.find("#picdetails").val();
        var seriesId = body.find("#seriesId").val();
        var state = body.find("#state").val();
        var title = body.find("#title").val();
        if(title==null || title=="" || title.length<3)
        {
            alert("商品标题不能为空或太短!");
            return false;
        }
        var name = body.find("#name").val();
        if(name==null || name=="" || name.length<3)
        {
            alert("商品名称不能为空或太短!");
            return false;
        }
        var nameSort = body.find("#nameSort").val();
        if(nameSort==null || nameSort=="" || name.length<1)
        {
            alert("商品简称不能为空!");
            return false;
        }
        var code = body.find("#code").val();
        var supplier = body.find("#supplier").val();
        var suppliernoid = body.find("#suppliernoid").val();
        var nameDesc = body.find("#nameDesc").val();
        if(nameDesc==null || nameDesc=="" || nameDesc.length<10)
        {
            alert("商品描述不能为空或太短(16个字)!");
            return false;
        }
        var scope = body.find("#scope").val();
        var booking = body.find("#booking").val();
        var bookingTime = body.find("#bookingTime").val();
        var deliveryTime = body.find("#deliveryTime").val();

        var json = {
            "url": "Goods/Add",
            "parm":{"zoneId":zoneId,
                "cfId1":cfId1,
                "cfId2":cfId2,
                "cfId3":cfId3,
                "cfname":cfname,
                "zonename":zonename,
                "seriesId":seriesId,
                "state":state,
                "audit":1,
                "title":title,
                "name":name,
                "nameSort":nameSort,
                "code":code,
                "supplier":supplier,
                "suppliernoid":suppliernoid,
                "nameDesc":nameDesc,
                "pic":pic,
                "ipic":ipic,
                "piclist":piclist,
                "picdetails":picdetails,
                "updateTime":new Date(),
                "scope":scope,
                "booking":booking,
                "bookingTime":bookingTime,
                "deliveryTime":deliveryTime
            }
        };

        $.ajax({
            type: "POST",
            url: "/goodsjson",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(json),
            dataType: "json",
            async: false,
            success: function (data) {
                if (data.code == "000000") {
                    //添加商品规格
                    AddSpec(body,data.data.id,name);

                    //alert("提交成功!!");
                    // location.reload();//刷新数据
                    ReloadSelectData();
                }
                else {
                    alert(data.errorDetail);
                    return false;
                }
            },
            error: function (message) {
                alert("提交数据失败！"+message);
                return false;
            }
        });
        return true;
    }

    function checkRate(nubmer) {
        var re = /^[0-9]+.?[0-9]*$/; //判断字符串是否为数字 //判断正整数 /^[1-9]+[0-9]*]*$/
        if (!re.test(nubmer)) {
            return false;
        }
        return true;
    }


    /**
     *
     * 添加商品规格
     *
     */
    function AddSpec(body,goodsId,goodsName){

        goodsName=encodeURI(goodsName);
        var name = body.find("#name1").val();
        var nameSort = body.find("#nameSort").val();
        var code = body.find("#code1").val();
        var weight = body.find("#weight").val();
        var state = body.find("#state1").val();
        var ulimit = body.find("#ulimit").val();
        var sumlimit = body.find("#sumlimit").val();


        var inventory = body.find("#inventory").val();
        if(!checkRate(inventory))
        {
            alert("库存输入错误!");
            return false;
        }
        var alarm = body.find("#alarm").val();
        if(!checkRate(alarm))
        {
            alert("库存警报输入错误!");
            return false;
        }

        var supplierprice = body.find("#supplierprice").val();
        var activitySelf = body.find("#activitySelf").val();
        var ownerCommission = body.find("#ownerCommission").val();
        var activityCommission = body.find("#activityCommission").val();
        var pic = body.find("#pic1").val();

        var price = body.find("#price").val();
        var cost = body.find("#cost").val();
        var operat = body.find("#operat").val();
        var share = body.find("#share").val();
        var consumption = body.find("#consumption").val();
        var coupon = body.find("#coupon").val();
        var vouchers = body.find("#vouchers").val();
        var owner1 = body.find("#owner1").val();
        var owner2 = body.find("#owner2").val();
        var owner3 = body.find("#owner3").val();
        var owner4 = body.find("#owner4").val();


        var operator = body.find("#operatorId").val();
        var operations = body.find("#operations").val();
        var operationsPrice = body.find("#operationsPrice").val();
        var operatorPrice = body.find("#operatorPrice").val();
        var redEnvelope = body.find("#redEnvelope").val();
        var aloeVeraShop = body.find("#aloeVeraShop").val();
        var CooperationPrice = body.find("#CooperationPrice").val();
        var PreferentialPrice = body.find("#PreferentialPrice").val();
        var PreferentialLimited = body.find("#PreferentialLimited").val();
        var bookingPrice = body.find("#bookingPrice").val();
        var bookingLimited = body.find("#bookingLimited").val();

        var packSpec = body.find("#packSpec").val();
        var goodsUnit = body.find("#goodsUnit").val();
        var shareId = body.find("#shareId").val();
        var shareGoods = body.find("#shareGoods").val();
        var service = body.find("#service").val();
        var areaService = body.find("#areaService").val();
        var commission1 = body.find("#commission1").val();
        var commission2 = body.find("#commission2").val();
        var shareFaith = body.find("#shareFaith").val();
        var consumptionFaith = body.find("#consumptionFaith").val();
        var shareTourism = body.find("#shareTourism").val();
        var consumptionTourism = body.find("#consumptionTourism").val();



        var json = {
            "url": "Spec/Add",
            "parm":{"name":name,
                "packSpec":packSpec,
                "goodsUnit":goodsUnit,
                "shareId":shareId,
                "shareGoods":shareGoods,
                "service":service,
                "areaService":areaService,
                "commission1":commission1,
                "commission2":commission2,
                "shareFaith":shareFaith,
                "consumptionFaith":consumptionFaith,
                "shareTourism":shareTourism,
                "consumptionTourism":consumptionTourism,
                "nameSort":nameSort,
                "code":code,
                "goodsId":goodsId,
                "goodsName":goodsName,
                "weight":weight,
                "state":state,
                "audit":1,
                "ulimit":ulimit,
                "sumlimit":sumlimit,
                "inventory":inventory,
                "alarm":alarm,
                "supplierprice":supplierprice,
                "activitySelf":activitySelf,
                "ownerCommission":ownerCommission,
                "activityCommission":activityCommission,
                "pic":pic,
                "price":price,
                "cost":cost,
                "operat":operat,
                "share":share,
                "consumption":consumption,
                "coupon":coupon,
                "vouchers":vouchers,
                "owner1":owner1,
                "owner2":owner2,
                "owner3":owner3,
                "owner4":owner4,
                "operator":operator,
                "operations":operations,
                "operationsPrice":operationsPrice,
                "operatorPrice":operatorPrice,
                "redEnvelope":redEnvelope,
                "aloeVeraShop":aloeVeraShop,
                "CooperationPrice":CooperationPrice,
                "PreferentialPrice":PreferentialPrice,
                "PreferentialLimited":PreferentialLimited,
                "bookingPrice":bookingPrice,
                "bookingLimited":bookingLimited
            }
        };

        $.ajax({
            type: "POST",
            url: "/goodsjson",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(json),
            dataType: "json",
            async: false,
            success: function (data) {
                if (data.code == "000000") {

                }
                else {
                    alert(data.errorDetail);
                    return false;
                }
            },
            error: function (message) {
                alert("提交数据失败！"+message);
                return false;
            }
        });
        return true;
    }

    /**
     *
     * 修改商品
     *
     */
    function Edit(body)
    {
        var id = body.find("#id").val();

        var zoneId = body.find("#zoneId").val();
        if(zoneId==0)
        {
            alert("请选择商品专区");
            return false;
        }
        var cfId1 = body.find("#cfId1").val();
        var cfId2 = body.find("#cfId2").val();
        var cfId3 = body.find("#cfId3").val();
        if(cfId3==null || cfId3==0)
        {
            alert("请选择商品分类");
            return false;
        }
        var cfname = body.find("#cfname").val();
        var zonename = body.find("#zonename").val();
        var pic = body.find("#pic").val();
        var ipic = body.find("#ipic").val();
        var piclist = body.find("#picList").val();
        var picdetails = body.find("#picdetails").val();
        var seriesId = body.find("#seriesId").val();
        var state = body.find("#state").val();
        var title = body.find("#title").val();
        if(title==null || title=="" || title.length<3)
        {
            alert("商品标题不能为空或太短!");
            return false;
        }
        var name = body.find("#name").val();
        if(name==null || name=="" || name.length<3)
        {
            alert("商品名称不能为空或太短!");
            return false;
        }
        var nameSort = body.find("#nameSort").val();
        if(nameSort==null || nameSort=="" || name.length<1)
        {
            alert("商品简称不能为空!");
            return false;
        }
        var code = body.find("#code").val();
        var supplier = body.find("#supplier").val();
        var suppliernoid = body.find("#suppliernoid").val();
        var nameDesc = body.find("#nameDesc").val();
        if(nameDesc==null || nameDesc=="" || nameDesc.length<10)
        {
            alert("商品描述不能为空或太短(16个字)!");
            return false;
        }
        var scope = body.find("#scope").val();
        var booking = body.find("#booking").val();
        var bookingTime = body.find("#bookingTime").val();
        var deliveryTime = body.find("#deliveryTime").val();

        var json = {
            "url": "Goods/Set",
            "parm":{"id":id,
                "zoneId":zoneId,
                "cfId1":cfId1,
                "cfId2":cfId2,
                "cfId3":cfId3,
                "cfname":cfname,
                "zonename":zonename,
                "seriesId":seriesId,
                "state":state,
                "title":title,
                "name":name,
                "nameSort":nameSort,
                "code":code,
                "supplier":supplier,
                "suppliernoid":suppliernoid,
                "nameDesc":nameDesc,
                "pic":pic,
                "ipic":ipic,
                "piclist":piclist,
                "picdetails":picdetails,
                "updateTime":new Date(),
                "scope":scope,
                "booking":booking,
                "bookingTime":bookingTime,
                "deliveryTime":deliveryTime
            }
        };


        $.ajax({
            type: "POST",
            url: "/goodsjson",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(json),
            dataType: "json",
            async: false,
            success: function (data) {
                if (data.code == "000000") {
                    //alert("提交成功!!");
                    // location.reload();//刷新数据
                    ReloadSelectData();
                }
                else {
                    alert(data.errorDetail);
                    return false;
                }
            },
            error: function (message) {
                alert("提交数据失败！"+message);
                return false;
            }
        });
        return true;
    }

</script>

</body>
</html>